use std::{net::SocketAddr, time::Duration};

use clap::Parser;
use std::str::FromStr;
use telnet::{Event, Telnet};
/// Simple program to greet a person
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
    #[arg(long)]
    host: String,

    #[arg(long)]
    port: u16,

    #[arg(long, default_value_t = 3)]
    timeout: u64,
}
fn main() {
    let args = Args::parse();
    println!("telnet {}:{}", &args.host, &args.port);

    let mut telnet = Telnet::connect_timeout(
        &SocketAddr::from_str(&format!("{}:{}", args.host.to_owned(), args.port.clone())).unwrap(),
        256,
        Duration::from_secs(args.timeout.clone()),
    )
    .expect("Couldn't connect to the server...");

    loop {
        let event = telnet.read().expect("Read error");

        if let Event::Data(buffer) = event {
            // Debug: print the data buffer
            println!("{:?}", buffer);
            // process the data buffer
        }
    }
}
